/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.acceptance.aggregator;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;
import org.ourgrid.acceptance.util.aggregator.Req_602_Util;
import org.ourgrid.acceptance.util.aggregator.Req_604_Util;
import org.ourgrid.aggregator.AggregatorComponent;
import org.ourgrid.aggregator.interfaces.Aggregator;
import org.ourgrid.common.statistics.beans.aggregator.monitor.AG_PeerStatusChange;
import org.ourgrid.common.statistics.beans.ds.DS_PeerStatusChange;
import org.ourgrid.common.statistics.beans.status.PeerStatus;
import org.ourgrid.reqtrace.ReqTest;

/**
 * Tests AG_PeerStatusChange insertion on Aggregator Database.
 */
public class Req_604_Test extends AggregatorAcceptanceTestCase {

	Req_602_Util req_602_Util = new Req_602_Util(getComponentContext());
	
	Req_604_Util req_604_Util = new Req_604_Util(getComponentContext());
	
	/**
	 * Two different peers turns to status PeerStatus.UP
	 * Checks whether both changes where inserted in the database.
	 * 
	 * @throws Exception
	 */
	@ReqTest(test = "AT-604.1", reqs = "receive peer status change")
	@Test public void test_AT_604_1_ReceivePeerStatusChange() throws Exception {
		
		//Start an Aggregator
		AggregatorComponent component = req_602_Util.startAggregator();
		
		Aggregator aggregator = req_604_Util.getAggregator(component);
		
		List<DS_PeerStatusChange> statusChanges = new ArrayList<DS_PeerStatusChange>();
		
		DS_PeerStatusChange changeA = new DS_PeerStatusChange();
		changeA.setCurrentStatus(PeerStatus.UP);
		changeA.setPeerAddress("peerA@xmpp.ourgrid.org");
		changeA.setTimeOfChange(0L);
		changeA.setLastModified(0L);
		
		statusChanges.add(changeA);
		
		DS_PeerStatusChange changeB = new DS_PeerStatusChange();
		changeB.setCurrentStatus(PeerStatus.UP);
		changeB.setPeerAddress("peerB@xmpp.ourgrid.org");
		changeB.setTimeOfChange(1L);
		changeB.setLastModified(1L);
		
		statusChanges.add(changeB);
		
		aggregator.hereIsPeerStatusChangeHistory(statusChanges, 0L);
		
		List<AG_PeerStatusChange> changes = req_604_Util.getPeerStatusChanges(component);
		
		Assert.assertEquals(2, changes.size());
		Assert.assertNotSame(changes.get(0).getPeer(), changes.get(1).getPeer());
	}
	
	/**
	 * The same peer turns to status PeerStatus.UP and then turns to PeerStatus.DOWN
	 * Checks whether both changes where inserted in the database and the both changes point to the same AG_Peer.
	 * 
	 * @throws Exception
	 */
	@ReqTest(test = "AT-604.2", reqs = "receive peer status change same peer")
	@Test public void test_AT_604_2_ReceivePeerStatusChangeSamePeer() throws Exception {
		
		//Start an Aggregator
		AggregatorComponent component = req_602_Util.startAggregator();
		
		Aggregator aggregator = req_604_Util.getAggregator(component);
		
		List<DS_PeerStatusChange> statusChanges = new ArrayList<DS_PeerStatusChange>();
		
		DS_PeerStatusChange changeA = new DS_PeerStatusChange();
		changeA.setCurrentStatus(PeerStatus.UP);
		changeA.setPeerAddress("peerA@xmpp.ourgrid.org");
		changeA.setTimeOfChange(0L);
		changeA.setLastModified(0L);
		
		statusChanges.add(changeA);
		
		DS_PeerStatusChange changeB = new DS_PeerStatusChange();
		changeB.setCurrentStatus(PeerStatus.DOWN);
		changeB.setPeerAddress("peerA@xmpp.ourgrid.org");
		changeB.setTimeOfChange(1L);
		changeB.setLastModified(1L);
		
		statusChanges.add(changeB);
		
		aggregator.hereIsPeerStatusChangeHistory(statusChanges, 0L);
		
		List<AG_PeerStatusChange> changes = req_604_Util.getPeerStatusChanges(component);
		
		Assert.assertEquals(2, changes.size());
		Assert.assertSame(changes.get(0).getPeer(), changes.get(1).getPeer());
	}
	
}
